
clear all




use "D:\SCB_ConPol\Stata\FlerGen\Fodelseuppg.dta", clear
bysort LopNr: gen n=_N
keep if n==1
drop AterPnr SenPnr n

merge 1:m LopNr using "D:\SCB_ConPol\Stata\VD\Valdelt_1994rkl.dta", nogen
bysort LopNr: gen n=_N
keep if n==1
drop n
gen voted1994 = .
replace voted1994 = 0 if r==1|r==3
replace voted1994 = 1 if r==2|r==4|r==5|r==6
replace voted1994 = . if Rostratt == 0
drop llkkdddd r k l utlandsrost llkkdddd_hafte Rostratt

merge 1:m LopNr using "D:\SCB_ConPol\Stata\VD\Valdelt_2010.dta", nogen
bysort LopNr: gen n=_N
keep if n==1
drop n
gen voted2010 = .
replace voted2010 = 0 if r==1|r==3
replace voted2010 = 1 if r==2|r==4|r==5|r==6
replace voted2010 = . if r == 3
drop AterPNr SenPNr kommun_id distrikt_id llkkdddd r k l Rostratt

merge 1:m LopNr using "D:\SCB_ConPol\Stata\VD\Valdelt_2018.dta", nogen
bysort LopNr: gen n=_N
keep if n==1
drop n
rename Rrost voted2018
replace voted2018 = "" if Rostratt == "3"

destring voted2018, replace
drop AterPnr SenPnr llkkdddd Rostratt Krost Lrost

merge 1:m LopNr using "D:\SCB_ConPol\Stata\VD\Valdelt_2022.dta", nogen
bysort LopNr: gen n=_N
keep if n==1
drop n
rename RD voted2022
replace voted2022 = . if Rostratt == 3
drop AterPnr SenPnr llkkdddd Rostratt KF RF




merge 1:1 LopNr using "H:\ProjData\Geocluster\data\lgb1994.dta", nogen
gen year=1994
append using "H:\ProjData\Geocluster\data\lgb2010.dta"
replace year=2010 if year==.
append using "H:\ProjData\Geocluster\data\lgb2018.dta"
replace year=2018 if year==.
append using "H:\ProjData\Geocluster\data\lgb2022.dta"
replace year=2022 if year==.

gen voted=.
bysort LopNr: egen v94=max(voted1994)
bysort LopNr: egen v10=max(voted2010)
bysort LopNr: egen v18=max(voted2018)
bysort LopNr: egen v22=max(voted2022)
bysort LopNr: replace voted=v94 if year==1994
bysort LopNr: replace voted=v10 if year==2010
bysort LopNr: replace voted=v18 if year==2018
bysort LopNr: replace voted=v22 if year==2022
drop v94 v10 v18 v22

bysort LopNr: egen BirthYear=max(floor(FodArMan/100))
gen age=year-BirthYear
drop if age<19

save "H:\ProjData\LGB\main_data_rep.dta", replace
